package com.wbs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.wbs.mapper.TbItemMapper;
import com.wbs.model.TbGoods;
import com.wbs.model.TbGoodsExample;
import com.wbs.model.TbItem;
import com.wbs.model.TbItemExample;
import com.wbs.service.ItemService;
import com.wbs.vo.GoodsVO;
import com.wbs.vo.ItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 芝梦
 * @Description:
 * @DateTime: 2024/2/21 17:02
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private TbItemMapper tbItemMapper;



    //模糊查询
    @Override
    public List<ItemVO> conditionFound(TbItem tbItem) {
        //传唤为VO
        ItemVO itemVO = this.conversionVO(tbItem);
        //模糊查询
        List<TbItem> tbItems = tbItemMapper.selectByExample(this.buildExample(itemVO));
        //将结果转换为   List<ItemVO>
        return tbItems.stream().map(this::conversionVO).collect(Collectors.toList());
    }

    @Override
    public TbItem selectByPrimaryKey(Long skuId) {
        return tbItemMapper.selectByPrimaryKey(skuId);
    }

    @Override
    public void updateStockQuantities(TbItem tbItem) {
        tbItemMapper.updateByPrimaryKeySelective(tbItem);
    }

    /*辅助方法------------------------------------------------------------------*/
    // 构建查询条件
    private TbItemExample buildExample(ItemVO itemVO) {
        TbItemExample tbItemExample = new TbItemExample();
        //通过goodsId查询
        if (itemVO.getGoodsId()!=0 && itemVO.getGoodsId() != null){
            tbItemExample.createCriteria().andGoodsIdEqualTo(itemVO.getGoodsId());
        }
        return tbItemExample;
    }

    //转换为VO
    private ItemVO conversionVO(TbItem tbItem){
        ItemVO itemVo = new ItemVO();
        BeanUtils.copyProperties(tbItem,itemVo);
        return itemVo;
    }

}
